/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import ADLER32 from './adler32.js'
import { pam, numStr, binaryStr, string } from './arrData.js'
const forNumber2 = 1000;
const forNumber3 = 100;
/* Gets verify values based on byte array or buffer data */
function buf_test() {
    let result;
    let startTime = Date.now();
    for (let i = 0; i < 1000; i++) {
        result = ADLER32.buf(pam);
    }
    let endTime = Date.now();

    print(`alder32_Buf: ${endTime - startTime} ms`)
    if (result != 2136934718) throw Error(`utr != 2136934718`);
    // print(`byte: ${result + ''}`)
}

/* Gets verify value from a binary string (ucs-2 encoded character) */
function bstr_test_01() {
    let result;
    let startTime = Date.now();
    for (let i = 0; i < 1000; i++) {
        result = ADLER32.bstr(numStr);
    }
    let endTime = Date.now();
    print(`alder32_Bstr: ${endTime - startTime} ms`)
    if (result != '2130733703') throw Error(`bstr != '2130733703'`);
}
/* Gets verify value from a binary string (ucs-2 encoded character) */
function bstr_test_02() {
    let pam = "SheetJS";
    let result = ADLER32.bstr(pam);//176947863

    let endResult;
    let startTime = Date.now();
    for (let i = 0; i < 10000; i++) {
        let s = ADLER32.buf([83, 104]);// 数组校验后的值
        s = ADLER32.str("eet", s);  // js字符串校验的值
        endResult = ADLER32.bstr("JS", s);//二进制字符串校验的值
    }
    let endTime = Date.now();
   
    if (result != endResult) throw Error(`bstr != 176947863`);
    print(`alder32_Bstr(compare): ${endTime - startTime} ms`)
}
/* Gets verify value from a binary string (ucs-2 encoded character) */
function bstr_test_03() {
    let result;
    let startTime = Date.now();
    for (let i = 0; i < 10000; i++) {
        result = ADLER32.bstr({ number: 3, plain: 'string', block: 'two\nlines\n',number: 3, plain: 'string', block: 'two\nlines\n' }.toString());
    }
    let endTime = Date.now();
    if (result != '758777255') throw Error(`bstr != '758777255'`);
    print(`alder32_Bstr(Map): ${endTime - startTime} ms`)
    // print(`utr:${result}`);
}   
/* Gets verify value from a standard js string */
function str_test_01() {
    let result;
    let startTime = Date.now();
    for (let i = 0; i < forNumber2; i++) {
        result = ADLER32.str(string);
    }
    let endTime = Date.now();
    if (result != '1646233875') throw Error(`str != '1646233875'`);
    print(`alder32_Str: ${endTime - startTime} ms`)
}
function str_test_02() {
    let result;
    let startTime = Date.now();
    for (let i = 0; i < forNumber3; i++) {
        result = ADLER32.str(binaryStr);
    }
    let endTime = Date.now();
    if (result != '587170832') throw Error(`str != '587170832'`);
    print(`alder32_Str(binary): ${endTime - startTime} ms`)
}
// function print(s){
//     console.log(s)
// }
/*  Benchmark  */
function alder32Run() {

    buf_test();
    bstr_test_01();
    bstr_test_02();
    bstr_test_03();
    str_test_01();
    str_test_02();
}
let loopCountForPreheat = 1;
for (let i = 0; i < loopCountForPreheat; i++) {
    alder32Run();
}

ArkTools.waitAllJitCompileFinish();

alder32Run();
